import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);

//----------------------------------首页----------------------------------
const index = () => import('@/views/homePage/homePage.vue');

//----------------------------------海外房产----------------------------------
//美国新房——列表页
const USEstate = () => import('@/views/OverseasHouse/Estate/US/USEstate.vue');

//泰国新房——列表页
const THEstate = () => import('@/views/OverseasHouse/Estate/TH/THEstate.vue');

//希腊新房——列表页
const GREstate = () => import('@/views/OverseasHouse/Estate/GR/GREstate.vue');

//日本新房——列表页
const JPEstate = () => import('@/views/OverseasHouse/Estate/JP/JPEstate.vue');

//新房详情页
const EstateDetails = () => import('@/views/OverseasHouse/Estate/EstateDetails/EstateDetails.vue');

//美国二手房
const UsedUSEstate = () => import('@/views/OverseasHouse/UsedUSEstate.vue');

//----------------------------------房源对比----------------------------------
const HouseContrast = () => import('@/views/HouseContrast.vue');

//----------------------------------移民留学----------------------------------
//美国移民
const USImmigration = () => import('@/views/USImmigration.vue');

//美国留学——高中
const USStudyAbroadHighSchool = () => import('@/views/USStudyAbroad/USStudyAbroadHighSchool.vue');

//美国留学——本科
const USStudyAbroadUndergraduate = () => import('@/views/USStudyAbroad/USStudyAbroadUndergraduate.vue');

//美国留学——硕士
const USStudyAbroadMaster = () => import('@/views/USStudyAbroad/USStudyAbroadMaster.vue');

//希腊移民
const GRImmigration = () => import('@/views/GRImmigration.vue');

//希腊留学
const GRStudyAbroad = () => import('@/views/GRStudyAbroad.vue');

//欧洲移民
const EUImmigration = () => import('@/views/EUImmigration.vue');

//----------------------------------美元理财----------------------------------
// 海外保险
const USInsurance = () => import('@/views/USInsurance.vue');

// 美国基金
const USFund = () => import('@/views/USFund.vue');

//----------------------------------资讯·直播----------------------------------
// 美国资讯——一级列表页
const News = () => import('@/views/News/News.vue');

//美国资讯——二级列表页
const NewsSecondaryList = () => import('@/views/News/NewsSecondaryList.vue');

//美国资讯——详情页
const NewsDetails = () => import('@/views/News/NewsDetails.vue');

//海外直播——一级列表页
const Video = () => import('@/views/Video/Video.vue');

//海外直播——二级列表页（现场看房、展会活动）
const VideoSecondaryList = () => import('@/views/Video/VideoSecondaryList.vue');

//海外直播——现场看房详情页
const VideoHouseDetails = () => import('@/views/Video/VideoHouseDetails.vue');

//海外直播——展会活动详情页
const VideoExhibitionDetails = () => import('@/views/Video/VideoExhibitionDetails.vue');

//海外直播——华美优胜精彩报道详情页
const VideoHMYSGoodReportDetails = () => import('@/views/Video/VideoHMYSGoodReportDetails.vue');

//----------------------------------关于华美优胜----------------------------------
// 公司简介
const About = () => import('@/views/About.vue');

//历届展会——列表页
const Exhibition = () => import('@/views/Exhibition/Exhibition.vue');

//历届展会——详情页
const ExhibitionDetails = () => import('@/views/Exhibition/ExhibitionDetails.vue');

//成功案例——列表页
const SuccessfulCases = () => import('@/views/SuccessfulCases/SuccessfulCases.vue');

//成功案例——详情页
const SuccessfulCasesDetails = () => import('@/views/SuccessfulCases/SuccessfulCasesDetails.vue');

//置业顾问团队
const Counselor = () => import('@/views/Counselor.vue');

//联系我们
const ContactUs = () => import('@/views/ContactUs.vue');

//网站合作
const SiteCooperation = () => import('@/views/SiteCooperation.vue');

//招聘信息
const Jobs = () => import('@/views/Jobs.vue');

//隐私协议
const PrivacyAgreement = () => import('@/views/PrivacyAgreement.vue');

//百科常见问题——列表页
const Question = () => import('@/views/Question/Question.vue');

//百科常见问题——详情页
const QuestionDetails = () => import('@/views/Question/QuestionDetails.vue');

//----------------------------------帮我找房----------------------------------
const HelpMeFindHouse = () => import('@/views/HelpMeFindHouse.vue');

//----------------------------------购房问答----------------------------------
const HouseQA = () => import('@/views/HouseQA.vue');

//----------------------------------网站导航----------------------------------
const SiteNav = () => import('@/views/SiteNav.vue');

//----------------------------------城市详情页----------------------------------
const City = () => import('@/views/City/City.vue');

//----------------------------------个人设置页----------------------------------
const account = () => import('@/views/account/account.vue');
//个人设置
const personalSettings = () => import('@/views/account/personalSettings.vue');
//我的收藏
const myCollection = () => import('@/views/account/myCollection.vue');

//----------------------------------404页----------------------------------
const error_404 = () => import('@/views/errorPage/404.vue');

const routes = [
    //首页
    {path: '/', component: index, meta: {keepAlive: true}},

    //海外房产——美国新房——列表页
    {path: '/USEstate', component: USEstate, meta: {keepAlive: true}},

    //海外房产——泰国新房——列表页
    {path: '/THEstate', component: THEstate, meta: {keepAlive: true}},

    //海外房产——希腊新房——列表页
    {path: '/GREstate', component: GREstate, meta: {keepAlive: true}},

    //海外房产——日本新房——列表页
    {path: '/JPEstate', component: JPEstate, meta: {keepAlive: true}},

    //海外房产——美国、泰国、希腊新房——详情页
    {path: '/EstateDetails', component: EstateDetails, meta: {keepAlive: false}},

    //海外房产——美国二手房
    {path: '/UsedUSEstate', component: UsedUSEstate, meta: {keepAlive: true}},

    //房源对比
    {path: '/HouseContrast', component: HouseContrast, meta: {keepAlive: false}},

    //移民留学——美国移民
    {path: '/USImmigration', component: USImmigration, meta: {keepAlive: true}},

    //移民留学——美国留学——高中
    {path: '/USStudyAbroadHighSchool', component: USStudyAbroadHighSchool, meta: {keepAlive: true}},

    //移民留学——美国留学——本科
    {path: '/USStudyAbroadUndergraduate', component: USStudyAbroadUndergraduate, meta: {keepAlive: true}},

    //移民留学——美国留学——硕士
    {path: '/USStudyAbroadMaster', component: USStudyAbroadMaster, meta: {keepAlive: true}},

    //移民留学——希腊移民
    {path: '/GRImmigration', component: GRImmigration, meta: {keepAlive: true}},

    //移民留学——希腊留学
    {path: '/GRStudyAbroad', component: GRStudyAbroad, meta: {keepAlive: true}},

    //移民留学——欧洲移民
    {path: '/EUImmigration', component: EUImmigration, meta: {keepAlive: true}},

    //美元理财——海外保险
    {path: '/USInsurance', component: USInsurance, meta: {keepAlive: true}},

    //美元理财——美国基金
    {path: '/USFund', component: USFund, meta: {keepAlive: true}},

    //资讯·直播——美国资讯——一级列表页
    {path: '/News', component: News, meta: {keepAlive: true}},

    //资讯·直播——美国资讯——二级列表页
    {path: '/NewsSecondaryList/:type', name: 'NewsSecondaryList', component: NewsSecondaryList, meta: {keepAlive: false}},

    //资讯·直播——美国资讯——详情页
    {path: '/NewsDetails/:newsId', name: 'NewsDetails', component: NewsDetails, meta: {keepAlive: false}},

    //资讯·直播——海外直播——一级列表页
    {path: '/Video', component: Video, meta: {keepAlive: true}},

    //资讯·直播——海外直播——二级列表页（现场看房、展会活动）
    {path: '/VideoSecondaryList', component: VideoSecondaryList, meta: {keepAlive: false}},

    //资讯·直播——海外直播——现场看房详情页
    {path: '/VideoHouseDetails', component: VideoHouseDetails, meta: {keepAlive: false}},

    //资讯·直播——海外直播——展会活动详情页
    {path: '/VideoExhibitionDetails', component: VideoExhibitionDetails, meta: {keepAlive: false}},

    //资讯·直播——海外直播——华美优胜精彩报道详情页
    {path: '/VideoHMYSGoodReportDetails', component: VideoHMYSGoodReportDetails, meta: {keepAlive: false}},

    //关于华美优胜——公司简介
    {path: '/About', component: About, meta: {keepAlive: true}},

    //关于华美优胜——历届展会——列表页
    {path: '/Exhibition', component: Exhibition, meta: {keepAlive: true}},

    //关于华美优胜——历届展会详情页
    {path: '/ExhibitionDetails', component: ExhibitionDetails, meta: {keepAlive: false}},

    //关于华美优胜——成功案例——列表页
    {path: '/SuccessfulCases', component: SuccessfulCases, meta: {keepAlive: true}},

    //关于华美优胜——成功案例详情页
    {path: '/SuccessfulCasesDetails', component: SuccessfulCasesDetails, meta: {keepAlive: false}},

    //关于华美优胜——置业顾问团队
    {path: '/Counselor', component: Counselor, meta: {keepAlive: true}},

    //关于华美优胜——联系我们
    {path: '/ContactUs', component: ContactUs, meta: {keepAlive: true}},

    //关于华美优胜——网站合作
    {path: '/SiteCooperation', component: SiteCooperation, meta: {keepAlive: true}},

    //关于华美优胜——招聘信息
    {path: '/Jobs', component: Jobs, meta: {keepAlive: true}},

    //关于华美优胜——隐私协议
    {path: '/PrivacyAgreement', component: PrivacyAgreement, meta: {keepAlive: true}},

    //关于华美优胜——百科常见问题——列表页
    {path: '/Question', component: Question, meta: {keepAlive: true}},

    //关于华美优胜——百科常见问题——详情页
    {path: '/QuestionDetails', component: QuestionDetails, meta: {keepAlive: false}},

    //帮我找房
    {path: '/HelpMeFindHouse', component: HelpMeFindHouse, meta: {keepAlive: true}},

    //购房问答
    {path: '/HouseQA', component: HouseQA, meta: {keepAlive: true}},

    //网站导航
    {path: '/SiteNav', component: SiteNav, meta: {keepAlive: true}},

    //城市详情页
    {path: '/City', component: City, meta: {keepAlive: false}},

    //个人设置页 ,
    {
        path: '/account', component: account, redirect: 'personalSettings',
        children: [
            {path: '/personalSettings', component: personalSettings},   //个人设置
            {path: '/myCollection', component: myCollection},   //我的收藏
        ], meta: {keepAlive: false}
    },

    //404页
    {path: '/404', component: error_404, meta: {keepAlive: true}},
    {path: '*', component: error_404, redirect: '/404', meta: {keepAlive: true}},

];

//实例化router
const router = new VueRouter({
    routes,
    mode: 'history',
    base: '/',
    //解决使用keep-alive后，第二个页面可能继承第一个页面的滚动条的高度
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
});

// 未登录状态下，跳转到个人中心页面要重定向到首页
const notLogin = [
    '/account',
    '/personalSettings',
    '/myCollection',
];

//localStorage过期控制代码——获取local并判断是否过期
let getLocalStorage = function (key, expired) {
    let dataStr = localStorage.getItem(key);
    let dataObj = JSON.parse(dataStr);
    if (new Date().getTime() - dataObj.curTime > expired) {
        return false;
    }else{
        return dataObj.data;
    }
};

// 全局守卫
router.beforeEach(function (to, from, next) {
    //百度统计
    if (_hmt) {
        if (to.path) {
            _hmt.push(['_trackPageview', '/#'+ to.fullPath]);
        }
    }

    if (localStorage.userName && localStorage.userHeadPhoto) {
        //已登录状态

        // let dataObj = getLocalStorage('userName', 1000*10);//过期时间为10秒
        //let dataObj = getLocalStorage('userName', 1000*60);//过期时间为1分钟
        //let dataObj = getLocalStorage('userName', 1000*60*60);//过期时间为1小时
        //let dataObj = getLocalStorage('userName', 1000*60*60*24);//过期时间为24小时
        //let dataObj = getLocalStorage('userName', 1000*60*60*24*7);//过期时间为1周
        let dataObj = getLocalStorage('userName', 1000*60*60*24);
        // console.log(dataObj)

        //判断用户登录信息是否过期
        if (!dataObj) {
            Vue.prototype.$notification['warning']({
                message: '华美优胜提示您：',
                description: '您的登录信息已过期，3秒后为您跳转至首页，请重新登录！',
                duration: 6
            });

            setTimeout(() =>{
                localStorage.removeItem('userName');
                localStorage.removeItem('userHeadPhoto');
                window.location.href = '/';
            }, 3000);
        }

        next();//不拦截
    } else {
        //未登录状态
        //includes是检测某个数组是否包含给定的值，返回一个布尔值
        if(notLogin.includes(to.path)){
            next('/');
        }else{
            next();//不拦截
        }
    }
});

// 导出router实例，main.js使用
export default router;